import requests
import json
import os


class NotJsonException(Exception):
    def __init__(self, content_type):
        self.content_type = content_type
    def __str__(self):
        return "响应的内容类型不是json，而是{}".format(self.content_type)


def check_json(response):
    content_type = response.headers.get("Content-Type")
    if content_type and "application/json" in content_type:
        return True
    else:
        raise NotJsonException(content_type)


wenbo = os.environ["wenbo"]
sign, uuid, cookie, token = wenbo.split("#")


with open("question_bank.json", "r", encoding="utf-8") as f:
    question_bank = json.load(f)
    question_dict = {}
    for question in question_bank:
        question_dict[question["title"]] = question["options"]


headers = {
    "xweb_xhr": "1",
    "cookie": cookie,
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x6309080f)XWEB/8461",
    "Token": token,
    "Content-Type": "application/x-www-form-urlencoded",
    "Accept": "*/*",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://servicewechat.com/wx0607abd12886662b/27/page-frame.html",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9"
}

data = {
    "uuid": uuid,
    "sign": sign
}


get_url = "https://wbdh.jiiimo.cn/api/question/get"
post_url = "https://wbdh.jiiimo.cn/api/question/post"


end = 0


while not end:
    try:

        get_response = requests.get(get_url, headers=headers, data=data)

        check_json(get_response)

        get_data = get_response.json()["data"]
        title = get_data["title"]
        options = get_data["options"]

        print("题目：", title)
        print("选项：", options)

        answer = question_dict.get(title, random.choice(list(options.keys())))

        print("答案：", answer)

        data["id"] = get_data["id"]
        data["select"] = answer
        data["wyy"] = get_data["wyy"]
        data["wyy_point"] = get_data["wyy_point"]

        post_response = requests.post(post_url, headers=headers, data=data)

        check_json(post_response)

        post_data = post_response.json()["data"]
        if post_data["answer"][answer] == answer:
            print("回答正确")
        else:
            print("回答错误")
        end = post_data["end"] or post_data["allend"]
        if end:
            print("答题结束")
        else:
            print("继续答题")
    except requests.exceptions.RequestException as e:

        print("出现网络错误：", e)
        break
    except json.decoder.JSONDecodeError as e:

        print("出现JSON解析错误：", e)
        break
    except KeyError as e:

        print("出现键值错误：", e)
        break
    except NotJsonException as e:

        print("出现响应的内容类型不是json的错误：", e)
        break
    except Exception as e:

        print("出现其他错误：", e)
        break
